<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * The class to handle something about rights, including the representation
 * of the user's right, checking the specifi right. Grant and revoke the rights.
 * 
 * 用户的权限控制类.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Adds the 'rootProtection' feature.
 * 		 Adds the 'checkRoot' function.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_util.php';

// Rights definitions.
// The numbers mean in which position(alrealy binary) the rights are represented.
define('RIGHT_NULL',					0);
define('RIGHT_NEWS',					1);
define('RIGHT_PHOTOSET',				2);
define('RIGHT_DATA',					4);
define('RIGHT_ASSOCITION',				8);
define('RIGHT_PROFILE',					16);


class Rights
{
	/**
	 * The unsinged rights flag.
	 */
	var $flag = 0;
	
	/**
	 * Check if the root user.
	 */
	static function checkRoot($username)
	{
		if (in_array($username, slh_config('site', 'root'))) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Check if the super administrator.
	 * 
	 * @return bool, true if is, false else.
	 */
	static function rootProtection()
	{
		$user = slh_current_user();
		
		if ( !empty($user)) {
			if (in_array($user->username, slh_config('site', 'root'))) {
				return true;
			}
		}
		
		return false;
	} 
	
	/**
	 * Check the if the righs flag has corresponding seted bit.
	 * 
	 * @param $flag, unsigned int. 32 bits.
	 * @param $right, position(binary) in the right flag.
	 * 
	 * @return bool, true if had, false else.
	 */
	static function hasRight($flag, $right)
	{
		if ( !is_numeric($flag)) {
			return false;
		}
		
		if ( ($flag & $right)) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Grant a right to a flag.
	 * 
	 * @param $flag, unsigned int. 32 bits.
	 * @param $right, position(binary) in the right flag.
	 * 
	 * @return unsigned int, a granted right flag.
	 */
	static function grantRight($flag, $right)
	{
		if ( !is_numeric($flag)) {
			return false;
		}
		
		return ($flag | $right);
	}
	
	/**
	 * Revoke a right to a flag.
	 * 
	 * @param $flag, unsigned int. 32 bits.
	 * @param $right, position(binary) in the right flag.
	 * 
	 * @return unsigned int, a revoked right flag.
	 */
	static function revokeRight($flag, $right)
	{
		if ( !is_numeric($flag)) {
			return false;
		}
		
		return ($flag & ~$right);
	}
	
}
 
?>